package com.atguigu.tingshu.live.api;

import com.atguigu.tingshu.common.annotation.Login;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.live.service.LiveRoomService;
import com.atguigu.tingshu.model.live.LiveRoom;
import com.atguigu.tingshu.vo.live.LiveRoomVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/live/liveRoom")
@SuppressWarnings({"unchecked", "rawtypes"})
public class LiveRoomApiController {
    @Resource
    private LiveRoomService liveRoomService;

    @GetMapping("getCurrentLive")
    @Operation(summary = "查询直播间")
    @Login
    public Result<LiveRoom> getCurrentLive() {
        Long userId = AuthContextHolder.getUserId();
        LiveRoom liveRoom = liveRoomService.getOne(new LambdaQueryWrapper<LiveRoom>().eq(LiveRoom::getUserId, userId).gt(LiveRoom::getCreateTime, new Date()));
        return Result.ok(liveRoom);
    }

    @PostMapping("saveLiveRoom")
    @Operation(summary = "新增直播间")
    @Login
    public Result<LiveRoom> saveLiveRoom(@RequestBody LiveRoomVo liveRoomVo) {
        LiveRoom liveRoom = liveRoomService.saveLiveRoom(liveRoomVo);
        return Result.ok(liveRoom);
    }

    @GetMapping("getById/{id}")
    @Operation(summary = "根据id获取直播间")
    public Result<LiveRoom> getById(@PathVariable long id) {
        LiveRoom liveRoom = liveRoomService.getById(id);
        return Result.ok(liveRoom);
    }

    @GetMapping("findLiveList")
    @Operation(summary = "获取直播间列表")
    public Result<List<LiveRoom>> findLiveList() {
        List<LiveRoom> liveRoomList = liveRoomService.list(new LambdaQueryWrapper<LiveRoom>().gt(LiveRoom::getCreateTime, new Date()));
        return Result.ok(liveRoomList);
    }
}

